package com.graduate.examapp.view.fragment

import android.app.AlarmManager
import android.app.PendingIntent
import android.content.Context
import android.content.Intent
import android.net.ConnectivityManager
import android.net.NetworkInfo
import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import androidx.databinding.DataBindingUtil
import androidx.fragment.app.Fragment
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.graduate.examapp.R
import com.graduate.examapp.adapter.HistoryDataAdapter
import com.graduate.examapp.broadcast.AlarmBroadcastReciever
import com.graduate.examapp.databinding.FragmentDataBinding
import com.graduate.examapp.entity.UserInfoFactory
import com.graduate.examapp.viewmodel.DataViewModel
import java.time.LocalDateTime
import java.time.ZoneId

class DataFragment : Fragment() {
    private val dataViewModel: DataViewModel by lazy {
        val model = ViewModelProvider(this)[DataViewModel::class.java]
        model
    }

    private var binding: FragmentDataBinding? = null

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        Log.i("HomeFragment", "create")
        binding = DataBindingUtil.inflate<FragmentDataBinding>(
            inflater,
            R.layout.fragment_data,
            container,
            false
        )
        binding?.taskTimes = "0"
        //用户基础信息
        init()
        return binding?.root
    }

    override fun onResume() {
        super.onResume()
        request()
    }

    private fun init() {
        val recyclerView = binding?.root?.findViewById<RecyclerView>(R.id.ranker)
        //Log.d("DataFragment", recyclerView.toString())
        recyclerView?.layoutManager =  LinearLayoutManager(this.context, LinearLayoutManager.VERTICAL, false)
        val historyDataAdapter = HistoryDataAdapter()
        recyclerView?.adapter = historyDataAdapter
        dataViewModel.historyLiveData.observe(requireActivity()) {
            it?.let {
                historyDataAdapter.dataSource = it
                //recycleView?.adapter = taskRecycleViewAdapter
                historyDataAdapter.notifyDataSetChanged()
                //taskRecycleViewAdapter.dataSource = it as MutableList<ExamTask>
                Log.d("DataFragment", historyDataAdapter.itemCount.toString())
            }
        }

        dataViewModel.userHistoryLiveData.observe(requireActivity()) {
            binding?.taskTimes = it?.times.toString()
        }
    }

    private fun request() {
        val name = UserInfoFactory.loginData?.insName
        name?.let {
            val connectivityManager = this.context?.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
            val info: NetworkInfo? = connectivityManager.getActiveNetworkInfo()
            if (info != null && info.isAvailable()) {
                val name = info.getTypeName();
                Log.d("DataFragment", "发起网络请求获取 历史数据")
                dataViewModel.fetchHistoryData(it)
            } else {
                Toast.makeText(this.context, "网络连接不可用", Toast.LENGTH_LONG).show()
            }
        }
    }
}